<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属校区" prop="schoolId">
        <el-select size="small" v-model="schoolId" placeholder="请选择所属校区" clearable
                   :style="{width: '100%'}">
          <el-option v-for="(item, index) in schoolIdOptions" :key="index" :label="item.label"
                     :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item size="small" label="授课老师" prop="teacherId">
        <el-select v-model="queryParams.teacherId" placeholder="请选择课老师" clearable
                   :style="{width: '100%'}">
          <el-option v-for="(item, index) in teacherIdOptions" :key="index" :label="item.label"
                     :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级名称" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['school:class:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['school:class:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['school:class:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['school:class:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="52" align="center"/>
      <!--      <el-table-column label="主键" align="center" prop="classId"/>-->
      <el-table-column width="165" label="班级编号" align="center" prop="classNumber">
        <template slot-scope="scope">
          <span @click="openClassDetails(scope.row)" style="color: #787be8; cursor: pointer;">{{'> '+scope.row.classNumber}}</span>
        </template>
      </el-table-column>
      <el-table-column  width="165" label="班级名称" align="center" prop="className"/>
      <el-table-column label="所属校区" align="center" prop="schoolName"/>
      <el-table-column label="任课老师" align="center" prop="nickName"/>
      <el-table-column label="所授课程" align="center" prop="courseName"/>
      <!--      <el-table-column label="默认扣除课时" align="center" prop="classDeductHours" />-->
      <!--      <el-table-column label="助教老师" align="center" prop="assistantId" />-->
      <!--      <el-table-column label="班级备注" align="center" prop="classNote" />-->
      <el-table-column label="学生人数" align="center" prop="stuCount">
        <template slot-scope="scope">
          <span>{{scope.row.stuCount+'人'}}</span>
        </template>
      </el-table-column>
      <el-table-column  width="165" label="上课时间" align="center" prop="classTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['school:class:edit']"
          >修改
          </el-button>
          <!--    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-hasPermi="['school:class:remove']"
                    >删除
                    </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改所有班级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listClass, getClass, delClass, addClass, updateClass, exportClass} from "@/api/school/class";
import {getSchoolAll, getSchoolCourse} from "../../../api/school/school";

export default {
  name: "Class",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 所有班级表格数据
      classList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        className: null,
        schoolId: null,
        teacherId: null,
      },
      schoolIdOptions: [],
      teacherIdOptions: [],
      schoolId: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  watch: {
    schoolId(schoolId) {
      this.teacherIdOptions = [];
      this.queryParams.schoolId = schoolId;
      this.queryParams.teacherId = null;
      if (schoolId !== '') {
        this.getTeacherAndCourse();
      }
    }
  },
  created() {
    this.getList();
    this.getSchoolAllData();
  },
  methods: {
    //跳转班级详情
    openClassDetails(row){
      console.log(row);

      this.$router.push({
        path:"/school/classDetails",
        query:{
          classDetails:row
        }
      })
    },
    getSchoolAllData() {
      getSchoolAll().then(res => {
        if (res.code === 200) {
          const data = []
          res.data.forEach(item => {
            data.push({
              label: item.schoolName,
              value: item.schoolId
            })
          })
          this.schoolIdOptions = data;

        }
      })
    }, getTeacherAndCourse() {
      getSchoolCourse(this.schoolId).then(res => {
        if (res.code === 200) {
          const teacher = [];
          res.data.schoolTeacher.forEach(item => {
            teacher.push({
              label: item.nickName + ` (${item.phonenumber})`,
              value: item.userId
            })
          })
          this.teacherIdOptions = teacher;
        }
      })
    },
    /** 查询所有班级列表 */
    getList() {
      this.loading = true;
      listClass(this.queryParams).then(response => {
        this.classList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        classId: null,
        classNumber: null,
        className: null,
        classDeductHours: null,
        classTime: null,
        schoolId: null,
        courseId: null,
        teacherId: null,
        assistantId: null,
        classNote: null,
        stuCount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.schoolId = ''
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.classId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path: '/school/addClass'})
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const classId = row.classId || this.ids
      /*getClass(classId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改所有班级";
      });*/
      this.$router.push({
        path:"/school/classUpdate",
        query:{
          item:row
        }
      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.classId != null) {
            updateClass(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClass(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const classIds = row.classId || this.ids;
      this.$confirm('是否确认删除所有班级编号为"' + classIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delClass(classIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有所有班级数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportClass(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
